package main

import (
	"fmt"
	"sort"
)

func main() {
	/*
	map ,集合 ，无序的键值对这种数据类型
	键值对
	map[key] = value
	1 3 5 6
	3 5 6 2
	通过散列函数对我们的map
	0xc0001
	0xc0002
	0xc0003

	map key value  ==
	*/

	//map的定义
	//1.申明
	var mapVariables1 map[string]string
	//2.make
	mapVariables1 = make(map[string]string)
	mapVariables1["Monday"] = "周一"
	mapVariables1["Tuesday"] = "周二"
	fmt.Println(mapVariables1)

	//map这里的key是唯一的，不能重复
	mapVariables1["Monday"] = "大周一"
	fmt.Println(mapVariables1)
	//map len
	fmt.Println(len(mapVariables1))

	//申明的时候同时赋值
	var mapVariables2 = map[string]int{
		"Monday":1,"Tuesday":2,"Wednesday":3,
		"Thursday":4,
	}
	fmt.Println(mapVariables2)

	//通过 := + make来定义map
	mapVariables3 := make(map[string]int)
	mapVariables3["Monday"] = 1
	mapVariables3["Tuesday"] = 2
	mapVariables3["Wednesday"] = 3
	fmt.Println(mapVariables3)

	//切片，函数不能做为map的key
	//mapVariables4 := map[[]string]int{}

	mapVariables5 := map[int][]string{}
	var sliceString []string
	sliceString = make([]string,3)
	sliceString[0] = "波哥"
	sliceString[1] = "golang"
	sliceString[2] = "go语言体系课"
	mapVariables5[0] = sliceString
	fmt.Println(mapVariables5)

	//只申明没有make,报错panic: assignment to entry in nil map
	//var mapVariables6 map[string]string
	//mapVariables6["hello"] = "波哥"
	//nil null 空

	//map是一个引用
	fmt.Printf("mapVariables3['Monday']在调用函数之前的值:%v\n",mapVariables3["Monday"])
	changeMap(mapVariables3)
	fmt.Printf("mapVariables3['Monday']在调用函数之后的值:%v\n",mapVariables3["Monday"])

	//如何来对map的key进行有顺序输出 , for range
	//mapVariables3["Monday"] = 888
	//mapVariables3["Tuesday"] = 2
	//mapVariables3["Wednesday"] = 3

	var sortKeys []string
	for key := range mapVariables3{
		sortKeys = append(sortKeys,key)
	}

	fmt.Printf("排序前sortkeys的值=%v\n",sortKeys)

	//sort
	sort.Strings(sortKeys)
	fmt.Printf("排序后sortkeys的值=%v\n",sortKeys)

	for i :=0;i<len(sortKeys);i++ {
		fmt.Printf("mapVariables3[%s]=%d\n",sortKeys[i],mapVariables3[sortKeys[i]])
	}

	//结构体和c类似 定义一个结构体，做为map的值
	type course struct {
		courseName string //课程名称
		courseTime float32 //课程时长 单位分钟
		courseTeacher string //课程讲师
	}

	couser1 := course {
		"go语言体系课",300.3,"波哥",
	}

	courser2 := course {
		courseTeacher:"胡哥",
		courseTime:100.2,
		courseName:"如何变帅",
	}

	courser := make(map[string]course)
	courser["go"] = couser1
	courser["美容"] = courser2
	fmt.Println(courser)

	//map如何判断一个key是否存在
	value,ok := courser["go"]
	if ok {
		fmt.Println(value)
	} else {
		fmt.Println("no value")
	}

	if value,ok := courser["go"];ok {
		fmt.Println(value)
	}

	//map切片 map+切片
	//interface{} 可以把它当作万能类型
	var mapVariables6 []map[string]interface{}
	mapVariables6 = make([]map[string]interface{},2)
	mapVariables6[0] = make(map[string]interface{},2)
	mapVariables6[0]["name"] = "波哥"
	mapVariables6[0]["age"] = 18

	mapVariables6[1] = make(map[string]interface{},2)
	mapVariables6[1]["name"] = "胡哥"
	mapVariables6[1]["age"] = 16

	fmt.Println(mapVariables6)

	//作业
	//如何实现Map的key与value互换？

}

func changeMap(mapVariables map[string]int) {
	mapVariables["Monday"] = 888
}


